/**
 * 工具数据管理 Composable
 */

import type { Tool, QueryToolsParams, ToolsResponse } from '~/types'

/**
 * 使用工具数据
 */
export const useTools = () => {
  const { $api } = useNuxtApp()

  /**
   * 获取工具列表
   */
  const getTools = async (params?: QueryToolsParams): Promise<ToolsResponse> => {
    return await $api('/tools', { params })
  }

  /**
   * 获取单个工具（通过ID或slug）
   */
  const getTool = async (idOrSlug: number | string): Promise<Tool> => {
    return await $api(`/tools/${idOrSlug}`)
  }

  /**
   * 获取热门工具
   */
  const getPopularTools = async (limit = 10): Promise<Tool[]> => {
    return await $api('/tools/popular', { params: { limit } })
  }

  /**
   * 获取特色工具
   */
  const getFeaturedTools = async (limit = 10): Promise<Tool[]> => {
    return await $api('/tools/featured', { params: { limit } })
  }

  /**
   * 获取推广工具
   */
  const getSponsoredTools = async (limit = 5): Promise<Tool[]> => {
    return await $api('/tools/sponsored', { params: { limit } })
  }

  /**
   * 点赞工具
   */
  const upvoteTool = async (id: number): Promise<Tool> => {
    return await $api(`/tools/${id}/upvote`, { method: 'POST' })
  }

  return {
    getTools,
    getTool,
    getPopularTools,
    getFeaturedTools,
    getSponsoredTools,
    upvoteTool,
  }
}


